<!--公司卸货仓管员维护-->
<template>
  <div class="m-window">
      <div class="m-condition">
        <el-row :gutter="5">
          <el-form
            ref="headerRuleForm"
            label-width="70px"
            :inline-message="true"
            label-position="left"
            class="m-headerForm"
          >
            <el-col :span="6">
              <el-form-item label="公司" class="rightItem">
                <el-select
                  placeholder="请选择公司"
                  v-model="companyCode"
                  clearable>
                    <el-option
                      v-for="(item,index) in companyItem"
                      :key="index"
                      :label="item.companyName"
                      :value="item.companyCode">
                    </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="2" :offset="12">
              <el-button @click="queryWareHouseKeeper" type="primary" :disabled="queryDisabled">查询</el-button>
            </el-col>
            <el-col :span="2">
              <el-button @click="addWareHouseKeeper" type="primary" :disabled="addDisabled">新增</el-button>
            </el-col>
            <el-col :span="2">
              <el-button @click="editWareHouseKeeper" type="primary" :disabled="editDisabled">编辑</el-button>
            </el-col>
          </el-form>
        </el-row>
      </div>
      <!-- 表格数据 -->
      <div class="m-agTable m-bottom">
        <ag-grid-vue class="ag-theme-balham ag-table"
            :gridOptions="gridOptions"
            :columnDefs="columnDefs"
            :rowData="rowData"
            v-loading="loading"
            @rowDoubleClicked="showDialog">
        </ag-grid-vue>
      </div>
      <!-- 弹出框 -->
      <el-dialog 
        :title="dialogTitle" 
        :visible.sync="dialogFormVisible" 
        v-dialogDrag>
        <el-form :model="addWareHouseKeeperform" label-width="120px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="公司代码" >
                <el-select v-model="addWareHouseKeeperform.companyCode" placeholder="请选择公司代码" @change="companyChange">
                  <el-option 
                  v-for="(item,index) in companyItem"
                  :key="index"
                  :value="item.companyCode"
                  :label="item.companyCode">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="公司名称">
                <el-input placeholder="公司名称" v-model="addWareHouseKeeperform.companyName" readonly></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="卸货仓管">
                <el-input placeholder="请输入卸货仓管姓名" v-model="addWareHouseKeeperform.linkman" clearable ref="linkman"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系方式">
                <el-input placeholder="请输入联系电话" v-model="addWareHouseKeeperform.phone" clearable></el-input>
              </el-form-item>
            </el-col>
          </el-row>
         
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogCancel">取 消</el-button>
          <el-button type="primary" @click="dialogSubmit">保 存</el-button>
        </div>
      </el-dialog>
  </div>
</template>

<script>

import gridOptions from "lesso-common/public/gridOptions"
import constant from "@/public/constant"

export default {
    name:"companyUnloadingWarehouseKeeper",
    data(){
      return {
        
        queryDisabled:false,            //查询按钮可编辑
        addDisabled:false,              //新增按钮可编辑
        editDisabled:false,             //编辑按钮可编辑
        companyCode:"",                 //公司编码
        companyItem:[],                 //公司数组

        // 列表数据
        gridOptions:null,
        columnDefs:null,
        rowData:[],
        loading:false,

        //弹出框
        dialogTitle:"新增",             //标题
        dialogFormVisible:false,        //弹出框隐藏
        addWareHouseKeeperform:{        //节假日弹框数据
          companyCode:"",               //开始日期
          companyName:"",               //公司名称
          linkman:"",                   //卸货仓管
          phone:""                      //联系方式
        }

      }
    },

    beforeMount(){
      this.gridOptions = gridOptions()
      this.columnDefs=[
        {headerName:"公司代码",field:"companyCode",width:130,checkboxSelection: true,headerCheckboxSelection: true},
        {headerName:"姓名",field:"linkman",width:130},
        {headerName:"联系方式",field:"phone",width:180},
        {headerName:"状态",field:"state",width:130},
        {headerName:"公司名称",field:"companyName",width:230},
      ]

    },
    created(){
      this.searchCompany();
      this.queryWareHouseKeeper();
    },
    methods:{
        //公司查询
        searchCompany () {
          var obj = {
            state: 0
          };
          var that =this;
          this.$http.getCompanyInfo(obj).then(function (response) {
            var data = response.data;
            if (data.code == "1" && data.total>0) {
              let companyData=data.rows.splice(0);
              that.companyItem=companyData;
            } else {
              that.$alert(data.info, {
                showClose: false,
                confirmButton: "确定",
                customClass: "alertFailure"
              });
            }
          }).catch((err) => {
            that.$alert("查询公司失败", {
              showClose: false,
              confirmButton: "确定",
              customClass: "alertFailure"
            });
          })
      },

      //查询首页
      queryWareHouseKeeper(){
        let params ={};
        if(this.companyCode!==""){
          params={
            eqParams:{company_code:this.companyCode}
          }
        }
        this.loading=true;
        this.queryDisabled=true;
        this.$http.queryWarehouseKeeper(params)
        .then((res)=>{
          this.loading=false;
          this.queryDisabled=false;
          let data=res.data;
          if(data.code==1){
            let originData = data.rows.splice(0);
            this.rowData=originData;
          }else{
            this.rowData=[];
            this.$message({
              $message:"查询失败",
              type:"error"
            })
          }
        })
        .catch((err)=>{
          this.loading=false;
          this.queryDisabled=false;
          this.$alert("查询失败，请检查网络！", {
          confirmButtonText: "确定",
          customClass: "alertFailure"
          });
        })
      },
      //公司切换
      companyChange(){
        this.companyItem.forEach(item=>{
          if(this.addWareHouseKeeperform.companyCode==item.companyCode){
            this.addWareHouseKeeperform.companyName=item.companyName
          }
        });
      },

      //新增
      addWareHouseKeeper(){
        this.dialogFormVisible=true;
        this.dialogTitle="新增"
      },
      //编辑
      editWareHouseKeeper(){
        let selectRow=this.gridOptions.api.getSelectedRows();
        if(selectRow.length==1){
          this.dialogFormVisible=true;
          this.dialogTitle="编辑"
          this.addWareHouseKeeperform={...selectRow[0]};
        }else if(selectRow.length>1){
          this.$alert("只能选择一行数据编辑",{
            showClose: false,
            confirmButtonText: "确定",
            customClass: "alertPrompt"
          })
        }else{
          this.$alert("请选择要编辑的数据",{
            showClose: false,
            confirmButtonText: "确定",
            customClass: "alertPrompt"
          })
        }
      },
      // 弹框取消
      dialogCancel(){
        this.addWareHouseKeeperform={};
        this.dialogFormVisible=false;
      },
      //双击跳出编辑弹框
      showDialog(event){
        let selectRow=event.data;
        this.dialogFormVisible=true;
        this.dialogTitle="编辑"
        this.addWareHouseKeeperform={...selectRow};
      },

      // 弹框确认
      dialogSubmit(){
        let linkManLen=this.$refs['linkman'].textLength;
        let msg="";
        switch(true){ 
            case this.addWareHouseKeeperform.companyCode=="":
                msg="公司代码不能为空";
                break;
            case this.addWareHouseKeeperform.companyName=="":
                msg="公司名称不能为空";
                break;
            case this.addWareHouseKeeperform.linkman=="":
                msg="卸货仓管不能为空";
                break;
            case linkManLen>50:
                msg="卸货仓管不能超过50个字符";
                break;
            case this.addWareHouseKeeperform.phone=="":
                msg="联系方式不能为空";
                break;
            case /^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(this.addWareHouseKeeperform.phone)==false:
                msg="请输入正确手机号码";
                break;
        }
        if(msg!=""){
            this.$alert("提示："+msg, {
                showClose: false,
                confirmButtonText: "确定",
                customClass: "alertPrompt"
            });
            return;
        }
        let params={
          companyCode: this.addWareHouseKeeperform.companyCode,         //公司代号
          companyName: this.addWareHouseKeeperform.companyName,         //公司名称
          creator: this.$global.user.loginName,                         //创建人
          creatorId: this.$global.user.id,                              //创建人Id
          linkman:this.addWareHouseKeeperform.linkman,                  //仓管员
          phone:this.addWareHouseKeeperform.phone,                      //联系方式
          }
        if(this.dialogTitle=="新增"){
          this.addDisabled=true;
          this.$http.addWarehouseKeeper(params)
          .then(res=>{
            this.addDisabled=false;
            this.dialogFormVisible=false;
            let data = res.data;
            if(data.code==1){
              this.$message({
                message:data.info,
                type:"success"
            })
            this.addWareHouseKeeperform={};
            this.queryWareHouseKeeper();
            }else{
              this.$alert(`提示:${data.info}`, {
                showClose: false,
                confirmButtonText: "确定",
                customClass: "alertPrompt"
              });
            }
          })
          .catch(error=>{
              this.addDisabled=false;
              this.dialogFormVisible=false;
              this.$alert(error.data.info, {
                confirmButtonText: "确定",
                customClass: "alertFailure"
              });
          })
        }else if(this.dialogTitle=="编辑"){
          this.editdDisabled=true;
          params.id=this.addWareHouseKeeperform.id                  //编辑ID
          params.modifyer=this.$global.user.loginName,              //修改人
          params.modifyerId=this.$global.user.id,                   //修改人ID
          params.creatorId=this.addWareHouseKeeperform.creatorId    //创建人ID
          params.creator=this.addWareHouseKeeperform.creator,       //创建人
          this.$http.editWarehouseKeeper(params).then(res=>{
            this.editdDisabled=false;
            this.dialogFormVisible=false;
            let data = res.data;
            if(data.code==1){
              this.$message({
                message:data.info,
                type:"success"
            })
            this.addWareHouseKeeperform={};
            this.queryWareHouseKeeper();
            }else{
              this.addWareHouseKeeperform={};
              this.$alert(`提示:${data.info}`, {
                showClose: false,
                confirmButtonText: "确定",
                customClass: "alertPrompt"
              });
            }
          }).catch(error=>{
            this.editdDisabled=false;
            this.dialogFormVisible=false;
            this.addWareHouseKeeperform={};
            this.$alert(error.data.info, {
              confirmButtonText: "确定",
              customClass: "alertFailure"
            });
          })
        }
        
      },
    }
}
</script>

<style lang="less" scoped>

  
</style>